﻿using UnityEngine;
using System.Collections;

public class HUD : MonoBehaviour{

    [SerializeField]
    public UIToolkit uitoolkitMenus;
    [SerializeField]
    public UIToolkit uitoolkitInventory;
    [SerializeField]
    public UIToolkit uitoolkitFonts;
    [SerializeField]
    public UIToolkit uiToolKitItems;
    [SerializeField]
    public Animation _animation;

	private UIButton btnAction;
	private UIButton btnMenu;
	private UIButton btnInventory;
	
    private HUDViewModel vmHUD;
    private ChangedHud chHUD;
	private UIToolkit _utInventory;
	private UIToolkit _utFonts;
	private UIToolkit _utMenu;
    private UIToolkit _utItems;
    //public Animation _animation;

	public UIToolkit UtInventory
	{
		get { return _utInventory; } 
		set { _utInventory = value; } 
	}
	
	public UIToolkit UtFonts
	{
		get { return _utFonts; } 
		set { _utFonts = value; } 
	}
	
	public UIToolkit UtMenu
	{
		get { return _utMenu; } 
		set { _utMenu = value; } 
	}

    public UIToolkit UtItems
    {
        get { return _utItems; }
        set { _utItems = value; }
    }

    private void CreateVMHUD()
    {
        if (vmHUD == null)
        {
            vmHUD = new HUDViewModel();
            vmHUD.UtFont = uitoolkitFonts;//UtFonts;
            vmHUD.TkMenu = uitoolkitMenus;// UtMenu;
            vmHUD.TkItems = uiToolKitItems;// UtItems;
            vmHUD._animation = this._animation;
        }
    }

    private void CreateChangeHUD()
    {
        if (chHUD == null)
        {
            chHUD = new ChangedHud();
            chHUD.UtFonts = uitoolkitFonts;// this.UtFonts;
            chHUD.CreateHUDChanged();
            chHUD.UpdateHUD();
        }
    }

	public void CreateHUD(){
        CreateVMHUD();
        CreateChangeHUD();
		vmHUD.ChHUD= this.chHUD;
		CreateControls();
		PutActions();
		PositionMenu();
	}

	private void CreateControls()
	{
		btnAction = UIButton.create("puno.png","puno.png",0,0);
        btnMenu = UIButton.create("btnOption.png", "btnOption.png", 0, 0);
		btnInventory = UIButton.create("Bag.png","Bag.png",0,0);
	}
	
	private void PutActions()
	{
		btnAction.onTouchUp += vmHUD.DoAction;
		btnInventory.onTouchUp += vmHUD.OpenAndCloseInventory;
		btnMenu.onTouchUp +=vmHUD.OpenAndCloseSubMenu;
	}
	
	private void PositionMenu()
	{
		btnAction.positionFromBottomRight(0.1f,0.01f);
		btnMenu.positionFromTopRight(0.01f,0.15f);
		btnInventory.positionFromTopRight(0.15f,0.01f);
	}

    public void RefreshHud(){
        chHUD.UpdateHUD();
    }

    void Start()
    {
        CreateHUD();
    }

    
   void LateUpdate()
   {
       if (ScreenGeneral.ShowMessagePickUp)
       {
           ScreenGeneral.ShowMessagePickUp = false;
           StartCoroutine(Example());
       }
       if (!string.IsNullOrEmpty(ScreenGeneral.message))
       {
           StartCoroutine(ShowMessage());
       }
   }
    
   private IEnumerator Example()
   {
       RefreshHud();
       yield return new WaitForSeconds(20);
       ScreenGeneral.CanPickUp = false;
       RefreshHud();
   }

   private IEnumerator ShowMessage()
   {
       RefreshHud();
       ScreenGeneral.message = string.Empty;
       yield return new WaitForSeconds(3);
       RefreshHud();
   }
   
}
